﻿using StoreProject.Entity.OtherEntity;
using StoreProject.OtherUtil;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.SettingForms
{
    public partial class OrderPrinterSettingForm : Form
    {
        IniConfig iniConfig = null; 
        public OrderPrinterSettingForm()
        {
            InitializeComponent();
        }

        private void OrderPrinterSettingForm_Load(object sender, EventArgs e)
        {
            iniConfig = new IniConfig("printer_config.ini");
            if (IsExist())
            {
                Init();
            }
            else
            {
                SetPrinter();
            }
        }

        private void Init()
        {
            this.txtLandscape.Text = Convert.ToBoolean(iniConfig.Read("Landscape", "OrderPrinter")) ? "横向" : "竖向";
            this.txtRight.Text = iniConfig.Read("Right", "OrderPrinter");
            this.txtLeft.Text = iniConfig.Read("Left", "OrderPrinter");
            this.txtTop.Text = iniConfig.Read("Top", "OrderPrinter");
            this.txtBottom.Text = iniConfig.Read("Bottom", "OrderPrinter");
            this.txtPrintName.Text = iniConfig.Read("PrinterName", "OrderPrinter");

            this.txtPaperName.Text = iniConfig.Read("PaperName", "OrderPrinter");
            this.txtWidth.Text = iniConfig.Read("Width", "OrderPrinter");
            this.txtHeight.Text = iniConfig.Read("Height", "OrderPrinter");
        }

        public bool IsExist()
        {
            var flag = iniConfig.KeyExists("Landscape", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("PrinterName", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Top", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Bottom", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Left", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Right", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("PaperName", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("RawKind", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Width", "OrderPrinter");
            if (!flag)
                return false;
            flag = iniConfig.KeyExists("Height", "OrderPrinter");
            if (!flag)
                return false;
            return true;
        }

        /// <summary>
        /// 设置打印机参数保存到INI文件，并打印一次
        /// </summary>
        public void SetPrinter()
        {
            PrinterSettingEntity printerSettingEntity = new PrinterSettingEntity();
            PageSetting pageSetting = new PageSetting();
            PrinterSetting printerSetting = new PrinterSetting();
            //先把中变变量设置到控件中
            PageSetupDialog pageSetupDialog1 = new PageSetupDialog();
            pageSetupDialog1.Document = printDocument1;
            pageSetupDialog1.AllowMargins = true;
            pageSetupDialog1.AllowOrientation = true;
            pageSetupDialog1.AllowPaper = true;
            pageSetupDialog1.AllowPrinter = true;
            pageSetupDialog1.ShowHelp = true;
            if (pageSetupDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.DefaultPageSettings = pageSetupDialog1.PageSettings;
                pageSetting.Landscape = printDocument1.DefaultPageSettings.Landscape;

                pageSetting.PaperSize = new Entity.OtherEntity.PaperSize();
                pageSetting.PaperSize.PaperName = printDocument1.DefaultPageSettings.PaperSize.PaperName;
                pageSetting.PaperSize.RawKind = printDocument1.DefaultPageSettings.PaperSize.RawKind;
                pageSetting.PaperSize.Width = printDocument1.DefaultPageSettings.PaperSize.Width;
                pageSetting.PaperSize.Height = printDocument1.DefaultPageSettings.PaperSize.Height;

                pageSetting.Margin = new Margin();
                pageSetting.Margin.Top = printDocument1.DefaultPageSettings.Margins.Top;
                pageSetting.Margin.Bottom = printDocument1.DefaultPageSettings.Margins.Bottom;
                pageSetting.Margin.Left = printDocument1.DefaultPageSettings.Margins.Left;
                pageSetting.Margin.Right = printDocument1.DefaultPageSettings.Margins.Right;
                //printDocument1.DefaultPageSettings.Margins;
                printerSettingEntity.pageSetting = pageSetting;
            }
            else
                return;

            PrintDialog print = new PrintDialog();
            print.Document = printDocument1;
            print.AllowCurrentPage = true;
            print.AllowPrintToFile = true;
            print.AllowSelection = true;
            print.AllowSomePages = true;
            print.ShowHelp = true;
            if (print.ShowDialog() == DialogResult.OK)
            {
                printDocument1.PrinterSettings = print.PrinterSettings;
                printerSetting.PrinterName = printDocument1.PrinterSettings.PrinterName;
                printerSettingEntity.printerSetting = printerSetting;
            }
            else
                return;
            iniConfig.Write("Landscape", printerSettingEntity.pageSetting.Landscape == true ? "True" : "False", "OrderPrinter");
            iniConfig.Write("Top", printerSettingEntity.pageSetting.Margin.Top.ToString(), "OrderPrinter");
            iniConfig.Write("Bottom", printerSettingEntity.pageSetting.Margin.Bottom.ToString(), "OrderPrinter");
            iniConfig.Write("Left", printerSettingEntity.pageSetting.Margin.Left.ToString(), "OrderPrinter");
            iniConfig.Write("Right", printerSettingEntity.pageSetting.Margin.Right.ToString(), "OrderPrinter");
            iniConfig.Write("PrinterName", printerSettingEntity.printerSetting.PrinterName, "OrderPrinter");

            iniConfig.Write("PaperName", printerSettingEntity.pageSetting.PaperSize.PaperName, "OrderPrinter");
            iniConfig.Write("RawKind", printerSettingEntity.pageSetting.PaperSize.RawKind.ToString(), "OrderPrinter");
            iniConfig.Write("Width", printerSettingEntity.pageSetting.PaperSize.Width.ToString(), "OrderPrinter");
            iniConfig.Write("Height", printerSettingEntity.pageSetting.PaperSize.Height.ToString(), "OrderPrinter");
            Init();
            //打印的是中间变量
            //printDocument1.Print();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            SetPrinter();
        }
    }
}
